<?php
function EMACalc($start, $end, $days,$Symbol){
require_once("DatabaseConnect.php");
//database cnnection
$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
//EMA = Price(today) * weight + EMA(yesturday) * (1 – weight)
//today = $today, yesterday = $yesterday, Number = $day (number of days in EMA), weight = 2/($days+1)

//default value for days variable for when it is == to none
if($days=="none"){
$days=1;	
}

$EMAarray=array();
$EMA=0;
$yesturdaysEMA=0;
$avg=0;
$count=0;

$movingAvGstartDate="";

//get new start date: which is equal to startDate-days, inorder to obtain the correct moving day average for the users start date
$sql="SELECT * FROM `shares` WHERE `Symbol`='$Symbol' AND `Date`<'$start' Order by `Date` DESC Limit $days";

$movingAvGstartDateQuery=mysqli_query($con,$sql);
//iterate through the sql query, the last result is the date that we are looking to obtain
while($row=mysqli_fetch_array($movingAvGstartDateQuery)){
	$movingAvGstartDate=$row['Date'];
	
}
$range = mysqli_query($con,"SELECT * 
FROM  `shares` 
WHERE  `Symbol` =  '$Symbol'
AND  `Date` 
BETWEEN  '$movingAvGstartDate'
AND  '$end'");


while($row = mysqli_fetch_array($range)) {

	$count+=1;
	$avg+=$row['Close'];
	
	if($count>=$days){
		
		
		//Get the value of yesturday
		$sql= mysqli_query($con,"SELECT * FROM `shares` WHERE `Symbol`='0001.HK' AND `Date` < '$row[Date]' ORDER BY `Date` DESC LIMIT 1");
		while($sqlrow = mysqli_fetch_array($sql)) {
			$yesturday=$sqlrow['Close'];
		}
		//get EMA of yesturday
		if($count==50){
			$avg=$avg/$days;
			$yesturdaysEMA=$avg;
				
		}
		if($count>50){
			$yesturdaysEMA=$EMA;	
		}
		
		//get the value of today
		$today=$row['Close'];
		
		//Calcullate the weight
		$weight = 2/($days+1);
		//calcualte the EMA
		$EMA=$weight*($today-$yesturdaysEMA)+$yesturdaysEMA;
		//echo"$EMA<br>";
		array_push($EMAarray, $EMA);

}
  
}
	return $EMAarray;
}

function DateTestEMA($start, $end, $days,$Symbol){
require_once("DatabaseConnect.php");
//database cnnection
$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
//EMA = Price(today) * weight + EMA(yesturday) * (1 – weight)
//today = $today, yesterday = $yesterday, Number = $day (number of days in EMA), weight = 2/($days+1)

$EMAarray=array();
$EMA=0;
$yesturdaysEMA=0;
$avg=0;
$count=0;

$movingAvGstartDate="";

//get new start date: which is equal to startDate-days, inorder to obtain the correct moving day average for the users start date
$sql="SELECT * FROM `shares` WHERE `Symbol`='$Symbol' AND `Date`<'$start' Order by `Date` DESC Limit $days";

$movingAvGstartDateQuery=mysqli_query($con,$sql);
//iterate through the sql query, the last result is the date that we are looking to obtain
while($row=mysqli_fetch_array($movingAvGstartDateQuery)){
	$movingAvGstartDate=$row['Date'];
	
}
$range = mysqli_query($con,"SELECT * 
FROM  `shares` 
WHERE  `Symbol` =  '$Symbol'
AND  `date` 
BETWEEN  '$movingAvGstartDate'
AND  '$end'");


while($row = mysqli_fetch_array($range)) {

	$count+=1;
	$avg+=$row['Close'];
	
	if($count>=$days){
		
		
		//Get the value of yesturday
		$sql= mysqli_query($con,"SELECT * FROM `shares` WHERE `Symbol`='$Symbol' AND `Date` < '$row[Date]' ORDER BY `Date` DESC LIMIT 1");
		while($sqlrow = mysqli_fetch_array($sql)) {
			$yesturday=$sqlrow['Close'];
		}
		//get EMA of yesturday
		if($count==50){
			$avg=$avg/$days;
			$yesturdaysEMA=$avg;
				
		}
		if($count>50){
			$yesturdaysEMA=$EMA;	
		}
		
		//get the value of today
		$today=$row['Close'];
		
		//Calcullate the weight
		$weight = 2/($days+1);
		//calcualte the EMA
		$EMA=$weight*($today-$yesturdaysEMA)+$yesturdaysEMA;
		//echo"$EMA<br>";
		array_push($EMAarray, $row['Date']);

}
  
}
	return $EMAarray;
}


?>